package de.fhhannover.inform.itteam.snmpnetviz;

import java.util.Iterator;
import java.util.List;
import java.util.concurrent.Callable;

import org.snmp4j.smi.OID;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.util.TableEvent;

import de.fhhannover.inform.itteam.snmpnetviz.gui.LoadingWindow;
import de.fhhannover.inform.itteam.snmpnetviz.snmp.SnmpAdapter;

public class snmpIpMacTableCallable implements Callable<IpToMac> {
	private String mIp;
	private LoadingWindow mLoadingWindow;
	
	public snmpIpMacTableCallable(String ip,LoadingWindow loadingWindow) {
		mIp = ip;
		mLoadingWindow = loadingWindow;
	}

	@Override
	public IpToMac call() throws Exception {
		mLoadingWindow.setStatusText("Read IP to MAC table from switch "+mIp+"...");
		SnmpAdapter snmp = new SnmpAdapter("udp:"+mIp+"/161", "public");
		
		IpToMac ipToMac = new IpToMac();
		
		// get the IP for MAC table from switch and store them local
		OID ipToMacTableOid = new OID("1.3.6.1.2.1.3.1.1.2");
		List<TableEvent> ipToMacTable = snmp.getTable(ipToMacTableOid);
		Iterator<TableEvent> ipToMacIt = ipToMacTable.iterator();
		while (ipToMacIt.hasNext()) {
			TableEvent next2 = ipToMacIt.next();
			if (next2.getColumns()!=null && next2.getColumns().length>0) {
				VariableBinding next = next2.getColumns()[0];
				
				String ipAddress = HelperFunctions.getIpFromOidString(next.getOid().toString());
				String macAddress = next.toValueString();
				
				ipToMac.addEntry(ipAddress, macAddress);
			}
		}
		
		return ipToMac;
	}

}
